<template>
	<view class="login">
		<view class="logo"><image src="../../static/logo.png"></image></view>
		<view class="list">
			<view class="item">
				<text>手机号</text>
				<input type="number" v-model="user" placeholder="请输入手机" />
			</view>
			<view class="item">
				<text>密码</text>
				<input type="password" v-model="password" placeholder="请输入密码" />
			</view>
			<view class="item1">
				<text class="left" @click="tozhuce">快速注册</text>
				<text @click="toforget">忘记密码？</text>
			</view>
		</view>
		<view class="tijiao" @click="tologin">登录</view>
		<!-- <view class="zhiwen" @tap="fingerprint()" v-if="!disabled && loginval">指纹登陆</view> -->
	</view>
</template>

<script>
var _this;
var timmer;
import { MQTTconnect, closeConnect } from '@/socket.js';
import { mapState } from 'vuex';
export default {
	data() {
		return {
			user: '',
			password: '',
			result: '',
			disabled: true,
			loginval: false,
			heartmsg: {}
		};
	},

	computed: {
		...mapState(['ifindex'])
	},
	onLoad() {
	
           
	
	},
	methods: {
		tozhuce() {
			uni.navigateTo({
				url: 'register'
			});
		},
		toforget() {
			uni.navigateTo({
				url: 'forget'
			});
		},
		tologin() {
			if (this.user == '' || this.password == '') {
				uni.showToast({
					title: '账号和密码不能为空',
					icon: 'none'
				});
			} else {
				uni.showLoading({
					title: '登录中'
				});
				var data = {
					bh: this.user,
					passwd: this.password
				};
				this.$store
					.dispatch('login', data)
					.then(res => {
						if (res.data.code == 1) {
							console.log('成功！');
							try {
								uni.setStorageSync('ifjiedan', 0);
							} catch (e) {}
							try {
								uni.setStorageSync('username', this.user);
								uni.setStorageSync('password', this.password);
							} catch (e) {
								// error
							}
							var data1 = {
								token: res.data.data.token
							};
							try {
								uni.setStorageSync('token', res.data.data.token);
							} catch (e) {}
                            console.log(data1)
							this.$store
								.dispatch('uerinfo', data1)
								.then(res => {
									console.log(res)
									if (res.data.code == 1) {
										try {
											uni.setStorageSync('is_auto', res.data.data.info.is_auto);
										} catch (e) {}

										var data1 = '20;' + res.data.data.info.yh_id + ';' + uni.getStorageSync('token') + ';';
										MQTTconnect(this, data1, res.data.data.info.yh_id, this.user, this.password);
										var data = {
											token: uni.getStorageSync('token')
										};
										this.$store.dispatch('orderunderway', data);
									}
								})
								
							clearInterval(timmer);
							this.heartmsg = {
								token: uni.getStorageSync('token')
							};
							timmer = setInterval(() => {
								this.$store.dispatch('heart', data1).then(res => {
									if (res.data.code == 0) {
										closeConnect();
										// #ifdef APP-PLUS
										_this.stopaudio();
										// #endif
									clearInterval(timmer);
									_this.$bus.$emit('cleartimern', 1);
									_this.$bus.$emit('cleartimer1', 1);
									_this.$bus.$emit('cleartimerq', 1);

										uni.showModal({
											title: '登陆过期',
											content: res.data.msg,
											showCancel: false,
											success: function(res) {
												uni.hideLoading();
												uni.removeStorage({
												    key: 'password',
												    success: function (res) {
												        
												    }
												});
												uni.reLaunch({
													url: '/pages/login/login'
												});
											}
										});
									} else {
									}
								});
							}, 5000);
						} else if (res.data.code == 0) {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							});
						}
					})

					.catch(err => {
						console.log(888);
						uni.hideLoading();
						uni.showToast({
							title: '服务器异常',
							icon: 'none'
						});
					});
			}
		},
		loginmsg() {
			uni.showLoading({
				title: '登录中'
			});
			var data = {
				bh: uni.getStorageSync('username'),
				passwd: uni.getStorageSync('password')
			};
			this.$store
				.dispatch('login', data)
				.then(res => {
					if (res.data.code == 1) {
						var data1 = {
							token: res.data.data.token
						};
						try {
							uni.setStorageSync('ifjiedan', 0);
						} catch (e) {}
						try {
							uni.setStorageSync('token', res.data.data.token);
						} catch (e) {}

						this.$store
							.dispatch('uerinfo', data1)
							.then(res => {
								console.log("登录成功")
								if (res.data.code == 1) {
									var data1 = '20;' + res.data.data.info.yh_id + ';' + uni.getStorageSync('token') + ';';
									MQTTconnect(_this, data1, res.data.data.info.yh_id, uni.getStorageSync('username'), uni.getStorageSync('password'));
									var data = {
										token: uni.getStorageSync('token')
									};
									this.$store.dispatch('orderunderway', data);
								}
							})
							
						clearInterval(timmer);
						this.heartmsg = {
							token: uni.getStorageSync('token')
						};
						timmer = setInterval(()=>{
							this.$store.dispatch('heart', this.heartmsg).then(res => {
								
								if (res.data.code == 0) {
									// #ifdef APP-PLUS
									_this.stopaudio();
									// #endif
									clearInterval(timmer);
									_this.$bus.$emit('cleartimern', 1);
									_this.$bus.$emit('cleartimer1', 1);
									_this.$bus.$emit('cleartimerq', 1);
									closeConnect();
									uni.showModal({
										title: '登录过期',
										content: res.data.msg,
										showCancel: false,
										success: function(res) {
											uni.reLaunch({
												url: '/pages/login/login'
											});
										}
									});
								}
							});
						}, 4000);
					} else if (res.data.code == 0) {
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						});
					}
				})

				.catch(err => {
					console.log(888);
					uni.hideLoading();
					uni.showToast({
						title: '服务器异常',
						icon: 'none'
					});
				});
		}
	},
	onLoad() {
		_this = this;
		// 订阅行情
		try {
			uni.getStorage({
				key: 'username',
				success: function(res) {
					if (res.data) {
						_this.user = res.data;
					}
				}
			});
			
		} catch (e) {
			//TODO handle the exception
		}
		this.$bus.$on('cleartimer', data => {
			clearInterval(timmer);
		});

		// #ifdef APP-PLUS
		let isIos = plus.os.name === 'iOS' ? 1 : 0;

		var phone = null;
		if (isIos) {
			phone = 'ios';
		} else {
			phone = 'android';
		}
		var oldversion = this.$version;

		console.log(oldversion);
		uni.request({
			url: 'http://mobile.suxin88.com:808/version.json?' + Math.random() * 10,
			success: res => {
				console.log(res.data[phone].version);
				if (res.data[phone].version != oldversion) {
					if (isIos) {
						var oldnum = oldversion.split('.')[0] * 100 + oldversion.split('.')[1] * 10 + oldversion.split('.')[2] * 1;
						var newnum = res.data[phone].version.split('.')[0] * 100 + res.data[phone].version.split('.')[1] * 10 + res.data[phone].version.split('.')[2] * 1;
						console.log(oldnum, newnum);
						var isupdateall = newnum - oldnum;
						if (!res.data[phone].wgtupdate || isupdateall > 5) {
							console.log(55555);
							uni.navigateTo({
								url: '../usercon/update/web-view?link=' +res.data[phone].url 
							});
						} else {
							uni.navigateTo({
								url: '../usercon/update/update'
							});
						}
						return;
					} else {
						console.log(6666);
						uni.navigateTo({
							url: '../usercon/update/update'
						});
					}
				}
				else{
					if (uni.getStorageSync('username') && uni.getStorageSync('password')) {
						console.log(uni.getStorageSync('username'),uni.getStorageSync('password'));
						  this.loginmsg();
						// this.loginval = true;
					}
				}
			},
			fail: err => {
				console.log(err);
			}
		});

		// #endif
	},
	watch: {
		heartmsg(newName, oldName) {
			console.log(newName);
		}
	}
};
</script>

<style lang="scss" scoped>
.login {
	width: 100%;
	height: 100%;
	.logo {
		text-align: center;
		image {
			width: 242rpx;
			height: 118rpx;
			margin: 155rpx 0 106rpx 0;
		}
	}
	.list {
		padding: 0 86rpx;
		.item {
			display: flex;
			justify-content: space-between;
			border-bottom: solid 2rpx #d4d4d4;
			padding: 44rpx 0 24rpx 0;
			text {
				color: #2b2b2b;
				font-size: 32rpx;
			}
			input {
				width: 454rpx;
				text-align: right;
				color: #808080;
				font-size: 32rpx;
			}
		}
		.item1 {
			display: flex;
			justify-content: space-between;
			padding-top: 20rpx;
			font-size: 26rpx;
			.left {
				color: #2296f3;
			}
		}
	}
	.tijiao {
		width: 450rpx;
		height: 80rpx;
		background-color: #2296f3;
		border-radius: 40rpx;
		font-size: 32rpx;
		color: #ffffff;
		margin: 0 auto;
		text-align: center;
		line-height: 80rpx;
		margin-top: 160rpx;
	}
	.zhiwen {
		text-align: center;
		padding-top: 15px;
		color: #2296f3;
	}
}
</style>
